//
//  SEPrescriptionRejectedViewController.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/4/21.
//

import UIKit

class SEPrescriptionRejectedViewController: SEBaseViewController {
    
    private var viewModel = SEPrescriptionViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewModel.updateData = { [weak self] status in
            self?.tableView.se_empty?.allowShow = true
            self?.endRefreshing()
            switch status {
            case .success:
                self?.tableView.reloadData()
            case .auth:
                self?.presentLoginViewController(selector: #selector(self?.loadData))
            case .failure: break
            }
        }
    }
    
    override func setupUI() {
        super.setupUI()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(UIScreen.navigationBarMaxY)
        }
    }

    // MARK: - getter
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .background
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 125
        tableView.rowHeight = UITableView.automaticDimension
        tableView.separatorStyle = .none
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.se_header = SERefreshHeader { [weak self] in self?.loadData() }
        tableView.se_header?.beginRefreshing()
        tableView.se_footer = SERefreshFooter { [weak self] in self?.loadData(more: true) }
        tableView.se_empty = SEEmptyView { [weak self] in self?.tableView.se_header?.beginRefreshing() }
        
        tableView.register(SEPrescriptionRejectedCell.nibForCell(),
                           forCellReuseIdentifier: SEPrescriptionRejectedCell.reuseIdentifier())
        
        return tableView
    }()

}

extension SEPrescriptionRejectedViewController {
    @objc private func loadData(more: Bool = false) {
        viewModel.getListOfPrescriptionRejected(more)
    }
    
    private func endRefreshing() {
        tableView.endHeaderRefreshing()
        tableView.endFooterRefreshing(hasMore: viewModel.hasMore)
    }
}

// MARK: - UITableViewDataSource

extension SEPrescriptionRejectedViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.numberOfRows
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEPrescriptionRejectedCell.reuseIdentifier()) as! SEPrescriptionRejectedCell
        cell.update(with: viewModel.viewModel(at: indexPath))
        return cell
    }
}

// MARK: - UITableViewDelegate

extension SEPrescriptionRejectedViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let vm = viewModel.viewModel(at: indexPath)
        pushToPrescriptionDetail(vm)
    }
    
    private func pushToPrescriptionDetail(_ vm: SEPrescriptionViewModel) {
        let prescription = SEPrescriptionMode.prescription(vm.prescription.bisId ?? "", vm.prescription.id ?? "", false)
        let vc = SEPrescriptionNewViewController(mode: prescription)
        navigationController?.pushViewController(vc, animated: true)
        vc.updateHandler = { self.loadData() }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        header.backgroundColor = .clear
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
}
